/**
 * Created by wangbin on 2017/11/22.
 */
$(function() {
	/*function loadProperties(){  
	    JQuery.i18n.properties({//加载properties文件  
	        name:'env.properties',//properties文件的名称  
	        path:'resources/env.properties',//properties文件的路径  
	        mode:'map',//用map的方式使用资源文件中的值  
	        callback:function(){  
	        	//alert($.i18n.prop("app.name"));//根据key值取得需要的资源  
	        }  
	    });  
	}*/

    var ctx = "/scm";
    var url = {
    		getOpenTicketFeeItems:ctx+"/incomeAcct/getOpenTicketFeeItems",
    		getChangeTicketFee:ctx+"/incomeAcct/getChangeTicketFee",
    		getExchangeRate:ctx+"/incomeAcct/getExchangeRate",
    		getTaxRateAmountList:ctx+"/incomeAcct/getTaxRateAmountList",
    		getInvoiceDept: ctx+"/incomeAcct/getInvoiceDept",
    		getFeeList: ctx+"/incomeAcct/getFeeList",
    		getopenTicketFeeType: ctx+"/incomeAcct/getopenTicketFeeType",
            genCheckRecord:ctx+"/incomeAcct/genCheckRecord",
            cancelRecord: ctx+"/incomeAcct/cancel",
            appendRecord:ctx+"/incomeAcct/append",
            listRecord:ctx+"/incomeAcct/getCheckList",
            listItems:ctx+"/incomeAcct/listItems",
            invalid:ctx+"/incomeAcct/invalid",
            export:ctx+"/incomeAcct/export"
     };
    
    var init = function() {
        $(".div-left-search").css("display","none");
        if(""!=$("#rmbAmount").val())
    	{
        	$("#invoiceCurRmb").attr("checked",true);
    	}
        if(""!=$("#usdAmount").val())
    	{
        	$("#invoiceCurUsd").attr("checked",true);
    	}
        
        var qryInvoiceDept = function () {
        	
        	var dept= $("#checkDept").val();
            $(top.callService("post",url.getInvoiceDept,null,function (data) {
            	
            	var arr =JSON.parse( data );
            	var myarr=eval(arr);             	
                for( var i=0; i<myarr.length; i++ ){  
                  if(dept==arr[i].check_dept_name)
                	  {
                	     $("#invoiceDept").append($("<option selected = true value="+myarr[i].check_dept+">"+arr[i].check_dept_name+"</option>")); 
                	  }else{
                		  $("#invoiceDept").append($("<option value="+myarr[i].check_dept+">"+arr[i].check_dept_name+"</option>"));  
                	  }
                }  
            },null,"html"));
        };
        qryInvoiceDept();
        
        var taxRateList = function(){
//        	$(top.callService("post",url.getTaxRateList,null,function (data) {
//            	
//            	var arr =JSON.parse( data );
//            	var myarr=eval(arr);             	
//                for( var i=0; i<myarr.length; i++ ){  
//                  
//                    $("#taxRateCombox").append($("<option value="+myarr[i].check_dept+">"+arr[i].check_dept_name+"</option>"));  
//                }  
//            },null,"html"));//暂时写死
        	var taxRate="[{text:'0%',val:'0.00'},{text:'3%',val:'0.03'},{text:'6%',val:'0.06'},{text:'11%',val:'0.11'},{text:'17%',val:'0.17'}]";
        	var taxRatearr=eval(taxRate);   
        	 for( var i=0; i<taxRatearr.length; i++ ){  
               
                 $("#taxRateCombox").append($("<option value="+taxRatearr[i].val+">"+taxRatearr[i].text+"</option>"));  
             }
        }
        	
        
       taxRateList();
       
       
       var taxRatedivList = function(){
//       	$(top.callService("post",url.getTaxRateList,null,function (data) {
//           	
//           	var arr =JSON.parse( data );
//           	var myarr=eval(arr);             	
//               for( var i=0; i<myarr.length; i++ ){  
//                 
//                   $("#taxRateCombox").append($("<option value="+myarr[i].check_dept+">"+arr[i].check_dept_name+"</option>"));  
//               }  
//           },null,"html"));//暂时写死
       	var taxRate="[{text:'0%',val:'0.00'},{text:'3%',val:'0.03'},{text:'6%',val:'0.06'},{text:'11%',val:'0.11'},{text:'17%',val:'0.17'}]";
       	var taxRatearr=eval(taxRate);   
       	 for( var i=0; i<taxRatearr.length; i++ ){  
              
                $("#taxRatediv").append($("<option value="+taxRatearr[i].val+">"+taxRatearr[i].text+"</option>"));  
            }
       }
      taxRatedivList();
       
       var feeNameList = function () {
    	   var param={};
    	   param.checkId=$("#checkId").val();
           $(top.callService("post",url.getFeeList,param,function (data) {
          
           	var arr =JSON.parse( data );
           	
               for( var i=0; i<arr.length; i++ ){  

                   $("#feeName").append($("<option  selectedIndex="+arr[i].index+" value="+arr[i].ID+">"+arr[i].costName+"</option>"));
                   
               }  
           },null,"html"));
       };
      feeNameList();     
       
    
       var invoiceTypeList = function(){
//       	$(top.callService("post",url.getInvoiceTypeList,null,function (data) {
//           	
//           	var arr =JSON.parse( data );
//           	var myarr=eval(arr);             	
//               for( var i=0; i<myarr.length; i++ ){  
//                 
//                   $("#invoiceType").append($("<option value="+myarr[i].invoiceType+">"+arr[i].invoiceTypeName+"</option>"));  
//               }  
//           },null,"html"));//暂时写死
       	var invoiceType="[{text:'普通发票',val:'1'},{text:'专用发票',val:'2'},{text:'专用收据',val:'3'},{text:'海关缴款书',val:'4'},{text:'铁路运单',val:'5'}]";
       	var invoiceTypeArr=eval(invoiceType);   
       	 for( var i=0; i<invoiceTypeArr.length; i++ ){  
              
                $("#invoiceType").append($("<option value="+invoiceTypeArr[i].val+">"+invoiceTypeArr[i].text+"</option>"));
                $("#invoiceTypeUsd").append($("<option value="+invoiceTypeArr[i].val+">"+invoiceTypeArr[i].text+"</option>"));  
            }
       }
       	
       
       invoiceTypeList();
       //获取对帐单下的人民币税率与金额列表
       var rmbTaxRateAmountList=function(){
    	 
    	var checkId=$("#checkId").val();
    	var param={};
    	param.checkId=checkId;
      	$(top.callService("post",url.getTaxRateAmountList,param,function (data) {
      	
     
      		
      	var arr =JSON.parse( data );
      	
      	var myarr=eval(arr);       
      	var html="";
          for( var i=0; i<myarr.length; i++ ){ 
        	
              html="<tr><td>"+myarr[i].costName+"</td>td class='center'  style='width: 100%;'>"+myarr[i].costName+"</td><td class='center' style='width: 30px;'>"+myarr[i].taxRate+"</td><td class='center'>"+myarr[i].amount+"</td></tr>";
             
              $("#simple-table-t").append(html);             
          }  
          $(top.callService("post",url.getOpenTicketFeeItems,param,function (data) {
        	  
        	 
        	  var arr =JSON.parse( data );
        	   
            	var myarr=eval(arr);    
            	
            	var html="";
                for( var i=0; i<myarr.length; i++ ){
                	
                   // html="<tr><td>"+myarr[i].costName+"</td>td class='center'  style='width: 100%;'>"+myarr[i].costName+"</td><td class='center' style='width: 30px;'>"+myarr[i].taxRate+"</td><td class='center'>"+myarr[i].amount+"</td></tr>";
                   
                    //$("#simple-table-invoice_items").append(html);    
                	var txtTRLastIndex = document.getElementById("txtTRLastIndex");
            		var rowID = parseInt(txtTRLastIndex.value);
            		
            		var tb=document.getElementById("simple-table-invoice_items");    //获取table对像
            		//添加行
            		var newTR = tb.insertRow(tb.rows.length);
            		newTR.id = "SignItem" + rowID;		
            		//发票名目
            		var newNameTD=newTR.insertCell(0);
            		
            		newNameTD.innerHTML = "<input name='feeTxt" + rowID + "' id='feeTxt" + rowID + "' type='text' style='width:80px' size='12' value='"+myarr[i].feeName+"' />";
            		
            		//税率
            		var newRateTD=newTR.insertCell(1);
            		
            		newRateTD.innerHTML = "<input name='taxRate" + rowID + "' id='taxRate" + rowID + "' type='text' style='width:80px' size='12' value='"+myarr[i].taxRate+"' />";
            		
            		//金额
            		var newPriceTD=newTR.insertCell(2);
            		//添加列内容
            		newPriceTD.innerHTML = "<input name='taxPrice" + rowID + "' id='taxPrice" + rowID + "' type='text' style='width:80px' size='20' value='"+myarr[i].taxPrice+"' />";
            		
            		//金额
            		var newContainerNumTD=newTR.insertCell(3);
            		//添加列内容
            		newContainerNumTD.innerHTML = "<input name='containerNum" + rowID + "' id='containerNum" + rowID + "' type='text' style='width:80px' size='20' value='"+myarr[i].containerNum+"' />";
            		
            		//金额
            		var newCurrencyTD=newTR.insertCell(4);
            		//添加列内容
            		newCurrencyTD.innerHTML = "<input name='currency" + rowID + "' id='currency" + rowID + "' type='text' style='width:80px' size='20' value='"+myarr[i].currency+"' />";
            		
            		//添加列:删除按钮
            		var newDeleteTD=newTR.insertCell(5);
            		//添加列内容
            		newDeleteTD.innerHTML = "<div align='center' style='width:40px'><a href='javascript:;' onclick=\"DeleteSignRow('SignItem" + rowID + "')\">删除</a></div>";
            		//将行号推进下一行
            		txtTRLastIndex.value = (rowID + 1).toString() ;	
                }  
          },null,"html"));
          
          
         },null,"html"));//暂时写死
      	
       }
       
       rmbTaxRateAmountList();
       //获取汇率
       var exchangeList=function(){
    	   
         	$(top.callService("post",url.getExchangeRate,null,function (data) {
             document.getElementById("exchangeRate").value=data; 
            },null,"html"));//暂时写死
          }
          
      exchangeList();
       
      var openTicketFeeType=function(){
    	  
    	$(top.callService("post",url.getopenTicketFeeType,null,function (data) {
    		var arr =JSON.parse( data );
    	
            for( var i=0; i<arr.length; i++ ){  
               
                $("#openTicketFeeType").append($("<option  value="+arr[i].id+">"+arr[i].feeName+"</option>"));
            } 
            $("#opentTicketTaxRate").val(arr[0].taxRate);
        },null,"html"));         
    	
        
    	
      }
     openTicketFeeType();
    }
   
     
  
    init();

    $(".js_btn_left_search").click(function () {
       $(".div-left-search").css("display","block");
    }) ;
    $(".js_left_query").click(function () {
       init();
    });
    /* 获取选中的下拉框的值 */  
//    var invoiceDeptSelected = function invoiceDeptSelected(){  
//        var valueSel = $("#invoiceDept").find("option:selected").val();  
//        var textSel = $("#invoiceDept").find("option:selected").text();  
//        console.info("选中的option的value值为："+valueSel);
//        console.info("选中的option的text值为："+textSel);
//    } 
});
//格式化保留两位小数
var fmt=  function format (x)
{

            var f = parseFloat(x);    
            if (isNaN(f)) {    
                return false;    
            }    
            var f = Math.round(x*100)/100;    
            var s = f.toString();    
            var rs = s.indexOf('.');    
            if (rs < 0) {    
                rs = s.length;    
                s += '.';    
            }    
            while (s.length <= rs + 2) {    
                s += '0';    
            }    
            return s;    
         
}
$("#adjustTaxRate").change(function() {
	
	var isChecked = $("#adjustTaxRate").prop("checked");
	if(isChecked){
		
		$("#batchUpdate").prop("checked",true);
	}else{
		$("#batchUpdate").prop("checked",false);
		$("#customFee").prop("checked",false);
	}
});
//删除指定行
function DeleteSignRow(rowid){
var signFrame = document.getElementById("simple-table-invoice_items");
var signItem = findObj(rowid,document);

//获取将要删除的行的Index
var rowIndex = signItem.rowIndex;

//删除指定Index的行
signFrame.deleteRow(rowIndex);

//重新排列序号，如果没有序号，这一步省略
for(i=rowIndex;i<signFrame.rows.length;i++){
signFrame.rows[i].cells[0].innerHTML = i.toString();
}
}
function findObj(theObj, theDoc)
{ 
var p, i, foundObj; 
if(!theDoc) theDoc = document; 
if( (p = theObj.indexOf("?")) > 0 && parent.frames.length) 
{    theDoc = parent.frames[theObj.substring(p+1)].document;    theObj = theObj.substring(0,p); } if(!(foundObj = theDoc[theObj]) && theDoc.all) foundObj = theDoc.all[theObj]; for (i=0; !foundObj && i < theDoc.forms.length; i++)     foundObj = theDoc.forms[i][theObj]; for(i=0; !foundObj && theDoc.layers && i < theDoc.layers.length; i++)     foundObj = findObj(theObj,theDoc.layers[i].document); if(!foundObj && document.getElementById) foundObj = document.getElementById(theObj);    return foundObj;
}

